﻿using MemberManagement.Common;
using MemberManagement.Context;
using MemberManagement.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MemberManagement
{
    public partial class RechargeHistoryForm : Form
    {
        public RechargeHistoryForm()
        {
            InitializeComponent();
            this.dataGridView1.Columns["colCreateTime"].DefaultCellStyle.Format = "yyyy-MM-dd hh:mm:ss";
        }

        private void RechargeHistoryForm_Load(object sender, EventArgs e)
        {
            this.dateTimePicker1.Value = DateTime.Now.AddYears(-1);
            this.dateTimePicker2.Value = DateTime.Now;
            this.RefreshData();
        }

        private List<RechargeQueryModel> LoadData(int currentPage, int pageSize, out int totalCount)
        {
            try
            {
                using (var db = new MyContext())
                {
                    var startTime = DateTime.Parse(this.dateTimePicker1.Value.ToString("yyyy-MM-dd 00:00:00"));
                    var endTime = DateTime.Parse(this.dateTimePicker2.Value.ToString("yyyy-MM-dd 23:59:59"));
                    var query = (from m in db.Member
                                 join r in db.Recharge on m.Id equals r.MemberId
                                 where r.CreateTime >= startTime && r.CreateTime <= endTime
                                 orderby r.CreateTime descending
                                 select new RechargeQueryModel
                                 {
                                     Id = r.Id,
                                     MemberName = m.Name,
                                     Type = r.Type,
                                     Balance = r.Balance,
                                     Amount = r.Amount,
                                     CreateTime = r.CreateTime,
                                     Remark = r.Remark,
                                     CardNumber = m.CardNumber,
                                     Mobile = m.Mobile
                                 }).AsQueryable();

                    if (!string.IsNullOrEmpty(txtName.Text))
                    {
                        query = query.Where(m => m.MemberName.Contains(txtName.Text));
                    }
                    if (!string.IsNullOrEmpty(this.txtCardNumber.Text.Trim()))
                    {
                        query = query.Where(m => m.CardNumber.Contains(this.txtCardNumber.Text));
                    }
                    totalCount = query.Count();
                    var result = query.Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();
                    return result;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                totalCount = 0;
                return new List<RechargeQueryModel>();
            }
        }

        private void RefreshData()
        {
            int totalCount = 0;
            var result = LoadData(this.pager.CurrentPage, this.pager.PageSize, out totalCount);
            this.pager.TotalCount = totalCount;
            this.dataGridView1.DataSource = result;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            this.RefreshData();
        }

        private void pager_EventPaging(EventArgs e)
        {
            this.RefreshData();
        }

        private void likExport_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.saveFileDialog1.FileName = DateTime.Now.ToString("yyyyMMddHHmm");
            if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    // 查出所有数据
                    using (var db = new MyContext())
                    {
                        var startTime = DateTime.Parse(this.dateTimePicker1.Value.ToString("yyyy-MM-dd 00:00:00"));
                        var endTime = DateTime.Parse(this.dateTimePicker2.Value.ToString("yyyy-MM-dd 23:59:59"));
                        var query = (from m in db.Member
                                     join r in db.Recharge on m.Id equals r.MemberId
                                     where r.CreateTime >= startTime && r.CreateTime <= endTime
                                     orderby r.CreateTime descending
                                     select new RechargeQueryModel
                                     {
                                         Id = r.Id,
                                         MemberName = m.Name,
                                         Type = r.Type,
                                         Balance = r.Balance,
                                         Amount = r.Amount,
                                         CreateTime = r.CreateTime,
                                         Remark = r.Remark,
                                         CardNumber = m.CardNumber,
                                         Mobile = m.Mobile
                                     }).AsQueryable();

                        if (!string.IsNullOrEmpty(txtName.Text))
                        {
                            query = query.Where(m => m.MemberName.Contains(txtName.Text));
                        }
                        if (!string.IsNullOrEmpty(this.txtCardNumber.Text.Trim()))
                        {
                            query = query.Where(m => m.CardNumber.Contains(this.txtCardNumber.Text));
                        }
                        var result = query.ToList();
                        Helper.ExportExcel<RechargeQueryModel>(result, GetExportColumns(), this.saveFileDialog1.FileName);
                    }
                    MessageBox.Show("导出成功", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private Dictionary<string, string> GetExportColumns()
        {
            return new Dictionary<string, string>
            {
                {"MemberName","姓名" },
                {"CardNumber","卡号" },
                {"Mobile","手机号" },
                {"TypeName","类型" },
                {"Amount","金额(元)" },
                {"Balance","余额(元)" },
                {"CreateTime","创建时间" },
                {"Remark","备注" },
            };
        }
    }
}
